#include "UIChat.h"
#include "EventPoller.h"
#include "Timer.h"
//#include "DX11InputManager.h"
#include "Game.h"
#include "TestState.h"
#include "CameraManager.h"
#include "DX11HelpfulFunctions.h"
#include "ServerSocket.h"
#include "ClientSocket.h"
#include "DX11ErrorReport.h"

#include <assert.h>

UIChat::UIChat()
{
	//Text* t = new Text();
	//t->Initialise();
	//delete t;

	m_takingInput = false;
	//TheEventPoller::Instance()->AddHandler(this);

	m_canTakingInput = Text("TakingInput");
	m_notTakingInput = Text("NotTakingInput");
	m_currentInput = Text("");

	m_canTakingInput.Initialise();
	m_notTakingInput.Initialise();
	m_currentInput.Initialise();

	m_pollKey = 0.00f;
	m_pollAmount = 0.01f;
	m_currentTimer = 0.0f;

	m_textPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_shiftDown = false;
}

UIChat::~UIChat()
{
	//TheEventPoller::Instance()->RemoveHandler(this);
}

void UIChat::Draw()
{
	DX11::ClearColour(0.5f, 0.5f, 0.5f, 1.0f);

	m_textPosition = D3DXVECTOR3(0.0f, -0.5f, 0.0f);


	for (int i = m_sentences.size(); i != 0; i--)
	{
		m_sentences[i - 1].Translate(m_textPosition);
		m_sentences[i - 1].Draw();
		m_textPosition.y += 0.1f;
	}

	if (m_sentences.size() > 5)
	{
		m_sentences.erase(m_sentences.begin());
	}

	//if (m_takingInput)
	//{
	//	m_canTakingInput.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	//	m_canTakingInput.Draw(1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	//}
	//else
	//{
	//	m_notTakingInput.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	//	m_notTakingInput.Draw(1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	//}

	m_currentInput.Update(m_currentSentence);
	m_currentInput.Translate(D3DXVECTOR3(0.0f, 0.3f, 0.0f));
	m_currentInput.Draw();
}

void UIChat::Update()
{
	TheCameraManager::Instance()->Update();

	m_currentTimer += TheTimer::Instance()->GetDt();
}

void UIChat::AddSentence(const std::string& s)
{
	m_sentences.push_back(Text(s));
}

void UIChat::OnKeyDownEvent(const WPARAM& event)
{
	if (event == VK_RETURN)
	{
		TheGame::Instance()->SetGameState(TheTestState::Instance());
	}
	else if (event == VK_BACK)
	{
		if (m_currentSentence.size() != 0)
		{
			m_currentSentence = std::string(m_currentSentence.begin(), m_currentSentence.end() - 1);
		}
	}
	else if (event == VK_SHIFT)
	{
		m_shiftDown = true;
	}
	else
	{
		char c = MapVirtualKey(event, MAPVK_VK_TO_CHAR);	
		if (m_shiftDown)
		{
			// special chars
			if (c == '2')
			{
				m_currentSentence += '@';
			}
			else if (c == ';')
			{
				m_currentSentence += ':';
			}
			else
			{
				m_currentSentence += c;
			}
		}
		else
		{
			if (c >= 'A' && c <= 'z')
			{
				m_currentSentence += (int)c + 32;
			}
			else
			{
				m_currentSentence += c;
			}
		}
	}
}

void UIChat::OnKeyUpEvent(const WPARAM& event)
{
	if (event == VK_SHIFT)
	{
		m_shiftDown = false;
	}
}

void UIChat::OnMouseMotionEvent(const LPARAM& event)
{

}

void UIChat::OnMouseButtonDownEvent(const MSG& event)
{

}

void UIChat::OnActivated()
{
	m_currentSentence = "";
	TheEventPoller::Instance()->AddHandler(this);
}

void UIChat::OnDeactivated()
{
	if (m_currentSentence != "")
	{
		if (m_currentSentence[0] == ':')
		{
			if (m_currentSentence[1] == ':')
			{
				Strings s = Split(m_currentSentence, ' ');
				// game codes
				if (s[0] == "::SERVER")
				{
					// create server
					//assert(s.size() == 2);
					if (s.size() < 2)
					{
						DX11ErrorMsg(L"UIChat: server needs port", L"UIChat Set Networking", MB_OK);
					}
					else
					{
						ServerSocket socket;
						socket.Open(ToInt(s[1]));
					}
				}
				else if (s[0] == "::CLIENT")
				{
					// create client
					assert(s.size() == 3);

					ClientSocket socket;
					socket.Open(s[1], ToInt(s[2]));

				}
			}
		}
		else
		{
			m_sentences.push_back(m_currentInput);
		}
	}
	TheEventPoller::Instance()->RemoveHandler(this);
}